package com.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.domain.cargo.ExtCproduct;
import com.domain.cargo.ExtCproductExample;
import com.domain.cargo.Factory;
import com.domain.cargo.FactoryExample;
import com.github.pagehelper.PageInfo;
import com.service.cargo.ExtCproductService;
import com.service.cargo.FactoryService;
import com.web.controller.BaseController;
import common.utils.UploadUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
//@DubboComponentScan
public class ExtCproductController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;


    @RequestMapping("/list")
//    cargo/extCproduct/list.do?contractId=${item.contractId}&contractProductId=${item.id}">[附件]</a>
    public String list(ExtCproductExample example,
                       @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size,
                       String contractId, String contractProductId){
        ExtCproductExample.Criteria criteria = example.createCriteria();
        criteria.andContractIdEqualTo(contractId);
        PageInfo pageInfo = extCproductService.findAll(example, page, size);
        request.setAttribute("page", pageInfo);

        //放入厂家
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);


        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId", contractProductId);
        return "/cargo/extc/extc-list";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id, String contractId, String contractProductId){
        //放入厂家
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct", extCproduct);

        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId", contractProductId);
        return "/cargo/extc/extc-update";
    }

    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto) throws IOException {
        extCproduct.setCompanyId(companyId);
        extCproduct.setCompanyName(companyName);

        //图片上传
        if (productPhoto != null) {
            String fileName = new UploadUtil().upload(productPhoto.getBytes());
            extCproduct.setProductImage(fileName);
        }

        if (StringUtils.isBlank(extCproduct.getId())) {
            extCproductService.save(extCproduct);
        }else{
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId=" + extCproduct.getContractId()
                + "&contractProductId=" + extCproduct.getContractProductId();
    }

    @RequestMapping("/delete")
    public String delete(String id, String contractId, String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId=" + contractId
                + "&contractProductId=" + contractProductId;
    }
}
